var err = true;

var flag = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var flag2 = [0, 0, 0, 0, 0, 0, 0, 0, 0];
var $$ = function(tagName) {
	return document.getElementsByTagName(tagName);
}

function showAlertBox(title, content){
	$(".layer").show();
	$(".alert-box .title h3").text(title);
	$(".alert-box .content p").text(content);
	$(".alert-box").removeClass("slide").addClass("shake").show();
}

function showRemind(obj,isok,iserror,content){
	var _obj = $(obj + "-icon");
	
	if(isok){
		_obj.addClass("input-ok");
	} else {
		_obj.removeClass("input-ok");
	}
	
	if (iserror) {
		_obj.addClass("input-error");
	} else {
		_obj.removeClass("input-error");
	}
	
	$(obj + "-msg").html(content);
	
}

// ---------用户名合法性检测
function check_name(user_name) {
	user_name = $.trim(user_name);
	$("#username").val(user_name);
	var reId = new RegExp("^[a-zA-Z0-9_]{3,20}$", "g");
	var b_id = reId.test(user_name);
	if (user_name.length == 0) {
		showRemind("#username", false , false , "<div class='notice-msg'>请输入6-16个字母和数字(不支持中文和特殊符号)</div>");
		err = true;
	} else if (user_name.length < 6 || (!b_id)) {
		showRemind("#username", false, true, "<div class='error-msg'>请输入6-16个字母和数字(不支持中文和特殊符号)</div>");
		flag[0] = 0;
		flag2[0] = 0;
		err = true;
	} else {
		checkid(user_name);
	}

}

function checkid(regid){
	$.ajax({
        type : "post",
        async : false,
        url : "/bind/regCheck", //实际上访问时产生的地址为: ajax.ashx?callbackfun=jsonpCallback&id=10
        data : "select=1&user=" + escape(regid),
        cache : false, //默认值true
        dataType : "json",
        success : function(json){
        	id_result(json);
        },
        beforeSend: function() {
        	showRemind("#username", false, false, "<div class='notice-msg'>&nbsp;正在进行校验...</div>");
		},
        error:function(){
        	showAlertBox("用户名", "用户名检测失败,请联系管理员");
        }
    });
}

function id_result(data) {
	var resultbox = document.getElementById("name");
	var openid = $("#openid").val();
	if (data == 1) {
		showRemind("#username", false, true, "<div class='error-msg'>你输入的用户名已经存在，重新输入或<a href='/bind/toIndex?openid=" + openid + "'>直接绑定</a></div>");
		flag[0] = 1;
		flag2[0] = 1;
		err = true;
	} else {
		showRemind("#username", true, false, "");
		flag[0] = 0;
		flag2[0] = 0;
		err = false;
	}

}
// ----------------------------------------------------------------------------------

function check_pass() {
	var pwd = $("#password").val();
	var reChinese = /[\u0391-\uFFE5]+/;
	var b_chinese = reChinese.test(pwd);
	var reSpace = /\s+/;
	var b_space = reSpace.test(pwd);
	// -------长度测试
	if (pwd == 0) {
		showRemind("#password", false, false, "<div class='notice-msg'>请输入6-16个字母，建议使用数字和符号，字母区分大小写</div>");
	} else if (pwd.length < 6) {
		showRemind("#password", false, true, "<div class='error-msg'>请输入6-16个字母，建议使用数字和符号，字母区分大小写</div>");
		flag[1] = 0;
		flag2[1] = 0;
		err = true;
	} else if (pwd > 0) {
		showRemind("#password", false, true, "<div class='error-msg'>该密码安全程度较低，有被盗风险，建议您更改为复杂密码，如字母+数字的组合</div>");
		flag[1] = 1;
		flag2[1] = 1;
		err = true;
	}
	// -------合法性检测:不能包含汉字
	else if (b_chinese) {
		showRemind("#password", false, true, "<div class='error-msg'>密码不能包含中文</div>");
		flag[1] = 0;
		flag2[1] = 0;
		err = true;
	}
	// -------合法性检测:不能包含空格
	else if (b_space) {
		showRemind("#password", false, true, "<div class='error-msg'>密码不能包含空格</div>");
		flag[1] = 0;
		flag2[1] = 0;
		err = true;
	}
	// -------合法时显示密码强度
	else {
		// 定义对应的消息提示		
		// check_pw();
		showRemind("#password", true, false, "");
		flag2[1] = 1;
		flag[1] = 1;
		err = false;
	}

}

// ---------密码一致性检测
function check_pw() {
	var pwd = $("#password").val().toString();
	var check_pwd = $("#repassword").val().toString();
	if (flag[1] == 1) {
		if (pwd == check_pwd) {
			if (pwd != "") {
				showRemind("#repassword", true, false, "");
			}
			flag[2] = 1;
			flag2[2] = 1;
			err = false;
		} else {
			showRemind("#repassword", false, true, "<div class='error-msg'>密码与确认密码不一致</div>");
			flag[2] = 0;
			flag2[2] = 0;
			err = true;
		}
	} else {
		if (pwd != check_pwd) {
			showRemind("#repassword", false, true, "<div class='error-msg'>密码与确认密码不一致</div>");
			flag[2] = 0;
			flag2[2] = 0;
			err = true;
		} else {
			if (pwd != "") {
				showRemind("#repassword", true, false, "");
			}
			flag[2] = 1;
			flag2[2] = 1;
			err = true;
		}
	}
}

// ----------------------------------------------------------------------------------
function check_company(com_name) {
	// com_name = trim(com_name);
	var s, i, j;
	k = 0;
	var companyTest = /^[A-Za-z\s+]*$/;
	s = "+=|'#&<>%*`^/\\\;《》，,。/？$!@_-?:。ǎ…￥~[]{}&—、 ";
	str01 = com_name;
	for (i = 0; i < str01.length; i++) {
		if (s.indexOf(str01.substring(i, i + 1)) >= 0) {
			k = 1;
			break;
		}
	}
	if (com_name.length == 0) {
		showRemind("#company", false, false, "<div class='notice-msg'>请填写工商局注册的公司全称，5-20个字。注册后不可再修改</div>");
	} else if (com_name.length < 5 || com_name.length > 30 || /\d/.test(com_name)) {
		showRemind("#company", false, true, "<div class='error-msg'>请填写工商局注册的公司全称，5-20个字。注册后不可再修改</div>");
		flag[3] = 0;
		flag2[3] = 0;
		err = true;
	} else if (companyTest.test(com_name)) {
		var array = com_name.split(" ");
		var arr = new Array();
		var count = 0;
		for (j = 0; j < array.length; j++) {
			if (array[j] != "") {
				arr[count] = array[j];
				count++;
			}
		}
		var comLen = 0;
		for (j = 0; j < com_name.length; j++) {
			if (com_name.charAt(j) == " ") {
				comLen++;
			}
		}
		var arrayLength = arr.length - 1;
		if (arrayLength != comLen) {
			showRemind("#company", false, true, "<div class='error-msg'>公司名称不正确,请检查公司名称是否包含多余的空格</div>");
			flag[3] = 0;
			flag2[3] = 0;
			err = true;
		} else {
			check_com(com_name);
		}
	} else if (k == 1) {
		showRemind("#company", false, true, "<div class='error-msg'>公司名称不正确,不能含有特殊符号，请核对在工商局注册的公司全称</div>");
		flag[3] = 0;
		flag2[3] = 0;
		err = true;
	} else {
		check_com(com_name);
	}

}

// --------------------------------------------------------------------
function check_com(com_name) {
	
	$.ajax({
        type : "post",
        async : false,
        url : "/bind/regCheck", //实际上访问时产生的地址为: ajax.ashx?callbackfun=jsonpCallback&id=10
        data : "select=2&company="+com_name,
        cache : false, //默认值true
        dataType : "json",
        success : function(json){
        	com_result(json);
        },
        beforeSend: function() {
        	showRemind("#company", false, false, "<div class='notice-msg'>&nbsp;正在进行校验...</div>");
		},
        error:function(){
        	showAlertBox("公司名称错误","公司名称检测失败,请联系管理员");
        }
    });
}

function com_result(data_com) {
	var resultbox = document.getElementById("com");
	if (data_com == 1) {
		showRemind("#company", false, true, "<div class='error-msg'>该公司名称已被注册，重新输入，或通过联系右方的<span style='color:blue'>在线客服</span>找回你公司的商铺账号</div>");
		flag[3] = 0;
		flag2[3] = 0;
		err = true;
	} else {
		showRemind("#company", true, false, "");
		flag[3] = 1;
		flag2[3] = 1;
		err = false;
	}
}

// -------------------联系人检测-----------------------------------------------------------------

function check_man(man_name) {
	man_name = trim(man_name);

	var s, i, j;
	k = 0;
	s = "123456789+=|'#&<>%*`^\\\;《》。，,？$!@_-?:。ǎ…￥~[]{}&— ";
	str01 = man_name;
	for (i = 0; i < str01.length; i++) {
		if (s.indexOf(str01.substring(i, i + 1)) >= 0) {
			k = 1;
			break;
		}
	}
	var manNameLength = 0;
	for (var i = 0; i < man_name.length; i++) {
		if (man_name.substr(i, 1).charCodeAt(0) > 255) {
			manNameLength += 2;
		} else {
			manNameLength++;
		}
	}

	var reChinese = /^[\u2E80-\u9FFF]+\/[\u2E80-\u9FFF]+$/;
	var b_chinese = reChinese.test(man_name);
	if (manNameLength == 0) {
		showRemind("#contact", false, false, "<div class='notice-msg'>2-8个字，或者4-16个英文字母，可由中文和英文组成</div>");
	} else if (manNameLength < 4 || manNameLength > 16) {
		showRemind("#contact", false, true, "<div class='error-msg'>2-8个字，或者4-16个英文字母，可由中文和英文组成</div>");
		flag[6] = 0;
		flag2[6] = 0;
		err = true;
	} else if (k == 1) {
		showRemind("#contact", false, true, "<div class='error-msg'>不能含有数字或特殊符号(只能中文和英文组成)</div>");
		flag[6] = 0;
		flag2[6] = 0;
		err = true;
	} else if (man_name.indexOf("/") != -1) {
		if (!b_chinese) {
			showRemind("#contact", false, true, "<div class='error-msg'>多个联系人请用'/'隔开</div>");
			flag[6] = 0;
			flag2[6] = 0;
			err = true;
		} else {
			showRemind("#contact", true, false, "");
			flag[6] = 1;
			flag2[6] = 1;
			err = false;
		}
	} else {
		showRemind("#contact", true, false, "");
		flag[6] = 1;
		flag2[6] = 1;
		err = false;
	}

}

// ---------------------
String.prototype.Trim1 = function() {
	var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);
	return (m == null) ? "" : m[1];
}

function trim(str) {
	var t = str.replace(/(^\s*)|(\s*$)/g, "");
	return t.replace(/(^ *)|( *$)/g, "");
}

function check_hd() {
	var phone = /(^0{0,1}1[3,4,5,8]{1}[0-9]{9}$)|(170[0-9]{8}$)|(^(170)[0-9]{8}$)/;
	var hd = $("#cellphone").val();
	if (hd !== "") {
		if (!phone.test(hd)) {
			showRemind("#cellphone", false, true, "<div class='error-msg'>手机号格式错误，请重新输入正确的手机号码，以便客户第一时间联系</div>");
			flag[12] = 0;
			flag2[12] = 0;
			err = true;
		} else {
			check_cellphone(hd);
		}

	} else {
		showRemind("#cellphone", false, false, "<div class='notice-msg'>请重新输入您的手机号码，手机号码为11位数字组成</div>");
		err = true;
	}
	
}



function check_cellphone(cellphone) {
	$.ajax({
        type : "post",
        async : false,
        url : "/bind/regCheck", //实际上访问时产生的地址为: ajax.ashx?callbackfun=jsonpCallback&id=10
        data : "select=4&hd="+cellphone,
        cache : false, //默认值true
        dataType : "json",
        success : function(json){
        	cellphone_result(json);
        },
        beforeSend: function() {
        	showRemind("#cellphone", false, false, "<div class='notice-msg'>&nbsp;正在进行校验...</div>");
		},
        error:function(){
        	showAlertBox("手机错误","手机检测失败,请联系管理员");
        }
    });
}

function cellphone_result(data_cellphone) {
	if (data_cellphone == 1) {
		showRemind("#cellphone", false, true, "<div class='error-msg'>你的手机号码已经被注册，如非本人注册请通过<span style='color:blue'>在线客服</span>寻求帮助完成注册</div>");
		flag[12] = 0;
		flag2[12] = 0;
		err = true;
	} else {
		showRemind("#cellphone", true, false, "");
		flag[12] = 1;
		flag2[12] = 1;
		err = false;
	}
}



function regSubmit(){ 
    var data = $("#regForm").serialize(); //序列化表单数据
    $.ajax({
        type : "post",
        async : false,
        url : "/bind/ajaxReg", //实际上访问时产生的地址为: ajax.ashx?callbackfun=jsonpCallback&id=10
        data : data,
        cache : false, //默认值true
        beforeSend: function() {
        	$("#reg-btn").html("信息提交中,请稍后..");
		},
        success : function(json){
        	json = eval("(" + json + ")");
        	
           if(json.result == 0){
            	window.location.href="/bind/regSuccess"
            		+ "?result=" + json.result
            		+ "&key=" + json.key
            		+ "&msg=" + json.msg;;
            }else if(json.result > 0){
            	showAlertBox("注册失败", json.msg);
            	$("#reg-btn").html("注册并绑定");
            	$("#reg-btn").click(function() {
            	        check_f();
            	});
            } else  if(json.result < 0){
            	window.location.href="/bind/regSuccess"
            		+ "?result=" + json.result
            		+ "&key=" + json.key
            		+ "&msg=" + json.msg;
            }
        },
        error:function(){
        	showAlertBox("错误", "注册失败，请联系客服！");
        	$("#reg-btn").html("注册并绑定");
        	$("#reg-btn").click(function() {
    	        check_f();
        	});
        }
    });
    function jsonpCallback(data) //回调函数
    {
        if(data){
        	if (data.result == 1) {
        		window.location.href="/bind/regSuccess";
        	} else {
        		
        	}
        }
    }
  
    return false; 
}


function check_f() {
	
	var readTerms = document.getElementById("agreement");
	if (!readTerms.checked) {
		$(".alert-box .content p").html("请阅读《一呼百应服务条款》并同意以上警告");
		$(".alert-box").removeClass("slide").addClass("shake").show();
		readTerms.focus();
		return false;
	}

	check_name($("#username").val());
	if (err) {
		$(".alert-box .content p").html("用户名错误，请按要求填写");
		$(".alert-box").removeClass("slide").addClass("shake").show();
		document.getElementById("username").focus();
		return false;
	}	
	
	check_pass();
	if (err) {
		$(".alert-box .content p").html("密码错误，请按要求填写");
		$(".alert-box").removeClass("slide").addClass("shake").show();
		document.getElementById("password").focus();
		return false;
	}

	check_company($("#company").val());
	if (err) {
		$(".alert-box .content p").html("公司名称错误，请按要求填写");
		$(".alert-box").removeClass("slide").addClass("shake").show();
		document.getElementById("company").focus();
		return false;
	}
	
	check_man($("#contact").val());
	if (err) {
		$(".alert-box .content p").html("联系人错误，请按要求填写");
		$(".alert-box").removeClass("slide").addClass("shake").show();
		document.getElementById("contact").focus();
		return false;
	}
	
	check_hd();
	if (err) {
		$(".alert-box .content p").html("手机号码错误，请按要求填写");
		$(".alert-box").removeClass("slide").addClass("shake").show();
		document.getElementById("cellphone").focus();
		return false;
	}

	$("#reg-btn").html("信息提交中,请稍后..");
	$("#reg-btn").unbind("click");
	
	regSubmit();
}